import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:repair_pda/pages/home/instruction_card.dart';
import 'package:repair_pda/pages/home/repair_task_card.dart';
import '../../controller/login_controller.dart';
import '../../widgets/loading_container.dart';

// 登录系统后的主页面
class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late final LoginController loginController;
  String userInfo = '';
  bool _loading = false;

  //#region appBar透明度
  double appBarAlpha = 0;
  static const appbarScrollOffset = 100;// appbar 从不透明到透明的滚动距离
  //#endregion

  @override
  void initState() {
    super.initState();
    loginController = Get.put(LoginController());
    // 确保loginModel已经初始化，否则需要添加null检查
    String flightsAndTeam = loginController.loginModel?.flights ?? '未知班次';
    String username = loginController.loginModel?.username ?? '未知的用户';
    userInfo = flightsAndTeam + ' ' + username;

    // 模拟异步请求，300毫秒后解除 loading 状态
    Future.delayed(Duration(milliseconds: 300), () {
      _loading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _appBar,
      backgroundColor: const Color(0xfff2f2f2),
      body: LoadingContainer(
          isLoading: _loading,
          cover: false,
          // child: Stack(children: [ _contentView, _appBar ]),
        child: ListView(
          children: [
            InstructionCard(),
            RepairTaskCard(status: '待维修', reporter: '范长龙', productionLine: 'XG-1', reportTime: '7月26日 19:11', device: '空压机', startTime: '7月26日 19:11', fault: '油路故障', endTime: ''),
            RepairTaskCard(status: '待维修', reporter: '范长龙', productionLine: 'XG-1', reportTime: '7月26日 19:11', device: '空压机', startTime: '7月26日 19:11', fault: '油路故障', endTime: ''),
            RepairTaskCard(status: '待维修', reporter: '范长龙', productionLine: 'XG-1', reportTime: '7月26日 19:11', device: '空压机', startTime: '7月26日 19:11', fault: '油路故障', endTime: ''),
          ],
        ),
      ),
    );
  }

  //#region appBar
  get _appBar => AppBar(
    title: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text('A早 吴阳阳'), // 左侧的文本
        Container(), // 占位空间，确保文本靠左
      ],
    ),
    actions: [
      TextButton(
        onPressed: () {
          // 按钮点击事件
          print('按钮被点击');
        },
        child: Text(
          '退出',
          style: TextStyle(color: Colors.black,fontSize: 20),
        ),
        style: TextButton.styleFrom(
          backgroundColor: Colors.transparent, // 扁平样式
        ),
      ),
    ],
  );
  //#endregion

  Future<void> _refreshPage() async {
    // TODO: 这里要写刷新页面数据的逻辑
    print('这里要写刷新页面数据的逻辑');
  }

  get _listView => ListView(
    children: [
      // TODO: 这里要放很多组件
      Text('这里要放很多组件')
    ],
  );

  void _onScroll(double offset){
    double alpha = offset / appbarScrollOffset;
    if(alpha < 0) alpha = 0;
    if(alpha > 1) alpha = 1;
    setState(() {
      appBarAlpha = alpha;
    });
  }
  // 刷新页面
  get _refreshContentView => RefreshIndicator(
      color: Colors.blue,
      onRefresh: _refreshPage,
      child: NotificationListener(
        child: _listView,
        onNotification: (scrollNotification){
          if(scrollNotification is ScrollUpdateNotification && scrollNotification.depth == 0){
            _onScroll(scrollNotification.metrics.pixels);
          }
          return false;// 不消费掉滚动事件
        },
      )
  );

  get _contentView => MediaQuery.removePadding(
    removeTop: true, // MediaQuery.removePadding#removeTop 移除顶部为灵动岛预留的留白空间
    context: context,
    child: _refreshContentView,
  );

  // Widget get _appBar => Row(
  //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //   children: [
  //     Text(userInfo,style: TextStyle(fontSize: 30)),
  //     CcButton('退出',onPressed: () => {
  //       Fluttertoast.showToast(
  //       msg: '退出登录状态了',
  //       toastLength: Toast.LENGTH_SHORT,
  //       gravity: ToastGravity.CENTER,
  //       timeInSecForIosWeb: 1,
  //       backgroundColor: Colors.red,
  //       textColor: Colors.white,
  //       fontSize: 16.0
  //       )
  //     })
  // ]);
}
